<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="产品类型" prop="dictId">
        <el-select v-model="queryParams.dictId" placeholder="请选择">
          <el-option
            v-for="dict in pr_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="coShow">
        <el-select v-model="queryParams.coShow" placeholder="请选择">
          <el-option label="启用" value="0"></el-option>
          <el-option label="禁用" value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="质保编码">
        <el-input v-model="queryParams.coNum" placeholder="按质保编码搜索" />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['applet:num:add']"
          >新增</el-button
        >
      </el-col>
<!--      <el-col :span="1.5">
        <el-upload
          :action="action"
          :headers="headers"
          :on-success="onSuccess"
          :show-file-list="false"
        >
          <el-button type="warning" plain icon="el-icon-upload2" size="mini"
            >导入</el-button
          >
        </el-upload>
      </el-col> -->
<!--      <el-col :span="1.5">
        <el-link
          class="loadMould"
          :href="handleDownload('/file/质保编码批量导入.xlsx')"
          download="质保编码批量导入.xlsx"
          :underline="false"
        >
          <el-button type="success" plain icon="el-icon-download" size="mini"
            >下载模板</el-button
          ></el-link
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="numList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="产品类型" align="center" prop="dictId">
        <template slot-scope="{ row }">
          {{ selectDictLabels(pr_list, String(row.dictId)) }}
        </template>
      </el-table-column>
      <el-table-column label="产品品牌" align="center" prop="brId">
        <template slot-scope="{ row }">
          {{ selectDictLabels(listBrand, String(row.brId)) }}
        </template>
      </el-table-column>
      <el-table-column label="产品型号" align="center" prop="prId">
        <template slot-scope="{ row }">
          {{ selectDictLabels(cpxhList, String(row.prId)) }}
        </template>
      </el-table-column>
      <el-table-column label="质保编码" align="center" prop="coNum" />
      <el-table-column label="加盟商" align="center" prop="leId" />
      <el-table-column label="是否启用" align="center" prop="coShow" />
      <el-table-column label="是否使用" align="center" prop="coGelten" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:num:edit']"
            >修改</el-button
          > -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:num:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改质保编码对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="产品类型" prop="dictId">
          <el-select v-model="form.dictId" placeholder="请选择">
            <el-option
              v-for="dict in pr_list"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value + ''"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品品牌" prop="brId">
          <el-select v-model="form.brId" placeholder="请选择">
            <el-option
              v-for="item in listBrand"
              :key="item.value"
              :label="item.label"
              :value="item.value + ''"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品型号" prop="prId">
          <el-select v-model="form.prId" placeholder="请选择">
            <el-option
              v-for="item in cpxhList"
              :key="item.value"
              :label="item.label"
              :value="item.value + ''"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择质保编码类型" prop="numType" v-if="editeNum">
          <el-radio-group v-model="form.numType" @change="handelNumType($event)">
            <el-radio :label="0">随机</el-radio>
            <el-radio :label="1">手动</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生成规则" prop="coRule" v-if="show">
          <el-input
            v-model="form.coRule"
            placeholder="请输入生成规则 开头字母或数字比如QM或001"
          />
        </el-form-item>
        <el-form-item label="加盟商" prop="leId">
          <el-select v-model="form.leId" placeholder="请选择">
            <el-option
              v-for="item in jmsList"
              :key="item.id"
              :label="item.leName"
              :value="item.leName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用" prop="coShow">
          <el-radio-group v-model="form.coShow">
            <el-radio :label="0">启用</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="位数" prop="coPlace" v-if="show">
          <el-input-number
            v-model="form.coPlace"
            :min="2"
            :max="1000"
            label="位数"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="个数" prop="coZahl" v-if="show">
          <el-input-number
            v-model="form.coZahl"
            :min="1"
            :max="1000"
            label="个数"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="排序" prop="coOrder" v-if="show">
          <el-input-number
            v-model="form.coOrder"
            :min="1"
            :max="1000"
            label="排序"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="分配的质保编码" prop="coNum" v-if="numShow">
          <el-input
            v-model="form.coNum"
            placeholder="请输入分配的质保编码"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNum, getNum, delNum, addNum, updateNum } from "@/api/applet/num";
import { listBrand } from "@/api/product/brand"; //产品类型
import { listModel } from "@/api/product/model"; // 产品型号
import { listNoPageLeague } from "@/api/applet/league"; // 加盟商
import { getToken } from "@/utils/auth";
import { getDicts } from "@/api/system/dict/data"

export default {
  name: "Num",
  dicts: ["product_type"],
  data() {
    return {
      show:true,
      numShow:false,
      editeNum:true,
      //导入文件
      action: this.$fileUrl1 + "/applet/num/importData",
      headers: {
        Authorization: "Bearer " + getToken(),
        "X-Project-ID": localStorage.getItem("projectId"),
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 质保编码表格数据
      numList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictId: null,
        coRule: null,
        coPlace: null,
        coZahl: null,
        brId: null,
        prId: null,
        leId: null,
        coSome: null,
        coShow: null,
        coOrder: null,
        coNum: null,
        numType:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      pr_list: [], //   产品类型
      listBrand: [], // 产品品牌
      cpxhList: [], // 产品类型
      jmsList: [], //加盟商
    };
  },
  created() {
    //获取字典数据
    this.getDicts("product_type").then((response) => {
      this.pr_list = response.data.map((item) => {
        return {
          label: item.dictLabel,
          value: item.dictCode,
        };
      });
    });
    this.getList();
    // 产品品牌
    listBrand(this.queryParams).then((res) => {
      this.listBrand = res.rows.map((res) => {
        return {
          label: res.brName,
          value: res.brName,
        };
      });
    });
    // 产品型号
    listModel(this.queryParams).then((res) => {
      this.cpxhList = res.rows.map((item) => {
        return {
          label: item.prModel,
          value: item.prModel,
        };
      });
    });
    // 加盟商
    listNoPageLeague(this.queryParams).then((res) => {
      // this.jmsList = res.rows.map((item) => {
      //   return {
      //     label: item.leShop,
      //     value: item.leShop,
      //   };
      // });
      console.log(2020,res.rows)
       this.jmsList=res.rows

    });
  },
  methods: {
    //导入文件成功后触发的钩子函数
    onSuccess(response, file, fileList) {
      if (response.code != 200) {
        this.$modal.msgError(response.msg);
      } else {
        this.$modal.msgSuccess(response.msg);
        this.getList();
      }
      this.$refs.upload.clearFiles();
      console.log(response);
    },
    //下载模板
    handleDownload(url) {
      return "https://" + window.location.host + url;
    },
    /** 查询质保编码列表 */
    getList() {
      this.loading = true;
      listNum(this.queryParams).then((response) => {
        response.rows.forEach((item) => {
          item.coShow = item.coShow == 1 ? "禁用" : "启用";
          item.coGelten = item.coGelten == 1 ? "已使用" : "未使用";
        });
        this.numList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dictId: null,
        coRule: null,
        coPlace: null,
        coZahl: null,
        brId: null,
        prId: null,
        leId: null,
        coSome: null,
        coShow: 0,
        coOrder: null,
        numType:0,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editeNum=true
      this.show=true
      this.numShow=false
      this.reset();
      this.open = true;
      this.title = "添加质保编码";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editeNum=false
      this.show=false
      this.numShow=true
      this.reset();
      const id = row.id || this.ids;
      getNum(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改质保编码";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateNum(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNum(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除质保编码编号为"' + ids + '"的数据项？')
        .then(function () {
          return delNum(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "applet/num/export",
        {
          ...this.queryParams,
        },
        `num_${new Date().getTime()}.xlsx`
      );
    },
    handelNumType(value){
      if(value==1){
        this.show=false
        this.numShow=true
      }else{
        this.show=true
        this.numShow=false
      }
    }
  },
};
</script>
